Aesthetics: Mapping information onto a plot

Elizabeth King
Kevin Middleton

This week

  1. Aesthetics
  2. Faceting and dodging
  3. Theming
  4. Accessibility

geom_s

  • The geometric building blocks of a plot
  • List of geom_s
  • Some geom_s are the building blocks of other geom_s
    • geom_smooth() adds a line based on a formula and some kind of regression
    • geom_rect() adds a box made of lines

Aesthetics: aes()

  • Map columns of data to elements (geom_s) on a plot
  • Some are common to many/most geom_s:
    • x, y, color, size, alpha, group
  • Others are geom-specific
    • xmin, xmax, xend, yend, linetype, fill, shape

Look to the ggplot function reference when in doubt.

geom_ribbon()

Thinking about aesthetics

  • Start with x and y
    • Or just one for univariate plots (histograms, densities, etc.)
    • Can be categorical variables (factors or characters coerced to factors within ggplot)
  • Then think about how to best encode other information
    • Color, shape, size, linetype, alpha
    • Facets (next lecture)

Working with aesthetics

Bee pollen removal experiment1

tibble [47 × 3] (S3: tbl_df/tbl/data.frame)
 $ Prop_removed: num [1:47] 0.07 0.1 0.11 0.12 0.15 0.19 0.28 0.31 0.3 0.34 ...
 $ Duration    : int [1:47] 2 5 7 11 12 11 9 9 16 17 ...
 $ Bee_type    : chr [1:47] "Queen" "Queen" "Queen" "Queen" ...


What kind of plot would best represent these data?

Working with color aesthetics

ggplot(bp,
       aes(x = Duration, y = Prop_removed, color = Bee_type)) +
  geom_point(size = 3)

Working with color aesthetics

Working with shape aesthetics

ggplot(bp,
       aes(x = Duration, y = Prop_removed, shape = Bee_type)) +
  geom_point(size = 3)

Working with shape aesthetics

Reversing the variables

ggplot(bp,
       aes(x = Prop_removed, y = Duration, color = Bee_type)) +
  geom_point(size = 3)

Reversing the variables

What is x and what is y?

By convention:

  • y is the variable of primary interest
  • x is what predicts y or the grouping of interest

Applying aesthetics

Generate some data to work with

set.seed(126633)
n <- 50
M <- tibble(
  x1 = runif(n, min = 0, max = 10),
  y = 2 * x1 + rnorm(n, mean = 0, sd = 1),
  x2 = sample(1:4, size = n, replace = TRUE),
  x3 = sample(LETTERS[1:4], size = n, replace = TRUE),
  x4 = runif(n, min = 0, max = 1)) |> 
  arrange(x1)
# A tibble: 6 × 5
      x1      y    x2 x3       x4
   <dbl>  <dbl> <int> <chr> <dbl>
1 0.0105  0.436     4 B     0.150
2 0.114  -2.35      2 C     0.917
3 0.223   1.04      3 D     0.912
4 0.398   0.370     2 D     0.168
5 0.666   0.871     2 D     0.608
6 0.671   1.83      2 C     0.353

Color by x2

ggplot(M,
       aes(x = x1, 
           y = y, 
           color = x2)) +
  geom_point(size = 5)

Color by x2

Color by x3

ggplot(M,
       aes(x = x1, 
           y = y, 
           color = x3)) +
  geom_point(size = 5)

Color by x3

Color by factor(x2)

ggplot(M,
       aes(x = x1, 
           y = y, 
           color = factor(x2))) +
  geom_point(size = 5)

Color by factor(x2)

Color by factor(x4)

ggplot(M,
       aes(x = x1, 
           y = y, 
           color = factor(x4))) +
  geom_point(size = 5)

Color by factor(x4)

Add a size aesthetic

ggplot(M,
       aes(x = x1,
           y = y, 
           color = x3, 
           size = x2)) +
  geom_point(size = 5)

Add a size aesthetic

Size

Unset size = 5 inside geom_point()

ggplot(M,
       aes(x = x1, 
           y = y,
           color = x3, 
           size = x2)) +
  geom_point()

Size

Automatic size scaling

ggplot(M,
       aes(x = x1, 
           y = y,
           color = x3,
           size = x4)) +
  geom_point()

Automatic size scaling

Shape

ggplot(M,
       aes(x = x1, 
           y = y, 
           shape = x3)) +
  geom_point(size = 5)

Shape

Shape

p <- ggplot(M,
            aes(x = x1, 
                y = y, 
                shape = x2)) +
  geom_point(size = 5)


Error in scale_f():

! A continuous variable can not be mapped to shape

Fill

Most often used for bars, including histograms:

ggplot(M,
       aes(x3, fill = x3)) +
  geom_bar()

Fill

Fill

We make this mistake every time:

ggplot(M,
       aes(x3, color = x3)) +
  geom_bar()

Fill

Fill

Can also fill point shapes (not all pch can be filled):

ggplot(M,
       aes(x = x1, y = y,
           color = x3,
           fill = factor(x2))) +
  geom_point(pch = 21, size = 10)

Fill

Linetype

ggplot(M,
       aes(x = x1,
           y = y,
           linetype = x3)) +
  geom_path()

Linetype

Alpha

alpha encodes transparency from 0 (transparent) to 1 (opaque)

ggplot(M,
       aes(x = x1,
           y = y,
           color = x3,
           alpha = x4)) +
  geom_point(size = 10)

Alpha

Setting an aesthetic vs. modifying a geom_

Compare:

...aes(x = x, y = y, color = x3, size = x4))
  + geom_point()

and

...aes(x = x, y = y))
  + geom_point(color = "darkred", size = 10)